package com.z3n.entities.controller;

import com.ibm.jpa.web.JPAManager;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import com.ibm.jpa.web.NamedQueryTarget;
import com.ibm.jpa.web.Action;
import com.z3n.entities.Z3nRelationship;
import java.util.List;
import javax.persistence.Query;

@SuppressWarnings("unchecked")
@JPAManager(targetEntity = com.z3n.entities.Z3nRelationship.class)
public class Z3nRelationshipManager {

	private EntityManagerFactory emf;

	public Z3nRelationshipManager() {
	
	}

	public Z3nRelationshipManager(EntityManagerFactory emf) {
		this.emf = emf;
	}

	public void setEntityManagerFactory(EntityManagerFactory emf) {
		this.emf = emf;
	}

	private EntityManager getEntityManager() {
		if (emf == null) {
			throw new RuntimeException(
					"The EntityManagerFactory is null.  This must be passed in to the constructor or set using the setEntityManagerFactory() method.");
		}
		return emf.createEntityManager();
	}

	@Action(Action.ACTION_TYPE.CREATE)
	public String createZ3nRelationship(Z3nRelationship Z3nRelationship)
			throws Exception {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			em.persist(Z3nRelationship);
			em.getTransaction().commit();
		} catch (Exception ex) {
			try {
				if (em.getTransaction().isActive()) {
					em.getTransaction().rollback();
				}
			} catch (Exception e) {
				ex.printStackTrace();
				throw e;
			}
			throw ex;
		} finally {
			em.close();
		}
		return "";
	}

	@Action(Action.ACTION_TYPE.DELETE)
	public String deleteZ3nRelationship(Z3nRelationship Z3nRelationship)
			throws Exception {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			Z3nRelationship = em.merge(Z3nRelationship);
			em.remove(Z3nRelationship);
			em.getTransaction().commit();
		} catch (Exception ex) {
			try {
				if (em.getTransaction().isActive()) {
					em.getTransaction().rollback();
				}
			} catch (Exception e) {
				ex.printStackTrace();
				throw e;
			}
			throw ex;
		} finally {
			em.close();
		}
		return "";
	}

	@Action(Action.ACTION_TYPE.UPDATE)
	public String updateZ3nRelationship(Z3nRelationship Z3nRelationship)
			throws Exception {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			Z3nRelationship = em.merge(Z3nRelationship);
			em.getTransaction().commit();
		} catch (Exception ex) {
			try {
				if (em.getTransaction().isActive()) {
					em.getTransaction().rollback();
				}
			} catch (Exception e) {
				ex.printStackTrace();
				throw e;
			}
			throw ex;
		} finally {
			em.close();
		}
		return "";
	}

	@Action(Action.ACTION_TYPE.FIND)
	public Z3nRelationship findZ3nRelationshipByRelationshipId(
			int relationshipId) {
		Z3nRelationship Z3nRelationship = null;
		EntityManager em = getEntityManager();
		try {
			Z3nRelationship = (Z3nRelationship) em.find(Z3nRelationship.class,
					relationshipId);
		} finally {
			em.close();
		}
		return Z3nRelationship;
	}

	@Action(Action.ACTION_TYPE.NEW)
	public Z3nRelationship getNewZ3nRelationship() {
	
		Z3nRelationship Z3nRelationship = new Z3nRelationship();
	
		return Z3nRelationship;
	}

	@NamedQueryTarget("getZ3nRelationshipByZ3nUser2")
	public List<Z3nRelationship> getZ3nRelationshipByZ3nUser2(
			Object z3nUser2_userId) {
		EntityManager em = getEntityManager();
		List<Z3nRelationship> results = null;
		try {
			Query query = em.createNamedQuery("getZ3nRelationshipByZ3nUser2");
			query.setParameter("z3nUser2_userId", z3nUser2_userId);
			results = (List<Z3nRelationship>) query.getResultList();
		} finally {
			em.close();
		}
		return results;
	}

	@NamedQueryTarget("getZ3nRelationshipByZ3nUser1")
	public List<Z3nRelationship> getZ3nRelationshipByZ3nUser1(
			Object z3nUser1_userId) {
		EntityManager em = getEntityManager();
		List<Z3nRelationship> results = null;
		try {
			Query query = em.createNamedQuery("getZ3nRelationshipByZ3nUser1");
			query.setParameter("z3nUser1_userId", z3nUser1_userId);
			results = (List<Z3nRelationship>) query.getResultList();
		} finally {
			em.close();
		}
		return results;
	}

	@NamedQueryTarget("getZ3nRelationshipByRelationshipId")
	public List<Z3nRelationship> getZ3nRelationshipByRelationshipId(
			int relationshipId) {
		EntityManager em = getEntityManager();
		List<Z3nRelationship> results = null;
		try {
			Query query = em
					.createNamedQuery("getZ3nRelationshipByRelationshipId");
			query.setParameter("relationshipId", relationshipId);
			results = (List<Z3nRelationship>) query.getResultList();
		} finally {
			em.close();
		}
		return results;
	}

}